/*
 * Copyright (C) 2012 mAPPn.Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mol.market.demo.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.mol.market.Constants;
import com.mol.market.R;
import com.mol.market.demo.widget.BaseFragmentActivity;
import com.mol.market.ui_v4.AppGeniusFragment;
import com.mol.market.ui_v4.TopicFragment;

/**
 * 第二层Activity，Back键回返回第一层Activity(即HomeFragmentActivity)
 * @author Chaos	
 * @Description 
 * 跳转到这个Activity的入口 (分发至Fragment显示) 如下：
 * <br>
 * 游戏-游戏分类  --> {@link CategoryFragment}
 * <br>
 * 游戏-精选标签  --> {@link TagFragment}
 * <br>
 * 应用-应用分类  --> {@link CategoryFragment}
 * <br>
 * 应用-精选标签  --> {@link TagFragment}
 * @date 2013-1-18 
 */
public class SecondFragmentActivity extends BaseFragmentActivity{

    private String mCurTag = null;
    private int    mRootId = 0;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        if(!checkTag()){
            //没有得到信息
            return ;
        }
        
        setContentView(createStupidView());
        
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        
        setFragment(mCurTag, getRootId());

        setActionBarText(mCurTag);
    }
    
    private ViewGroup createStupidView(){
        
        mRootId = createRandomId();
        
        if(findViewById(mRootId) != null){
            return (ViewGroup) findViewById(mRootId);
        }
        
        ViewGroup stupid = new FrameLayout(getApplicationContext());
        stupid.setMinimumWidth(0);
        stupid.setMinimumHeight(0);
        stupid.setId(mRootId);
        return stupid;
    }
    
    /**
     * FIXME ID 生成需要改变 
     */
    protected int createRandomId(){
        return 0x123456;
    }
    
    public int getRootId(){
        return mRootId;
    }
    
    private void setFragment(String tag,int contentId){
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        ft.replace(contentId, distribute(tag));
        
        ft.commit();
    }
    
    /**
     * 数量较小，手动映射跳转。
     * 当跳转页面过多时，建议使用Map进行自动映射。
     */
    protected Fragment distribute(String tag) {

        Fragment result = null;

        Bundle args = new Bundle();
        args.putString(KeyStore.KEY_ARG, tag);

        if (KeyStore.TARGET_CATEGORY_APP.equals(tag)) {
            result = Fragment.instantiate(getApplicationContext(), CategoryWithHeaderFragment.class.getName(), args);
        } else if (KeyStore.TARGET_CATEGORY_GAME.equals(tag)) {
            result = Fragment.instantiate(getApplicationContext(), CategoryFragment.class.getName(), args);
        } else if (KeyStore.TARGET_CATEGORY_TAG_APP.equals(tag)) {
            result = Fragment.instantiate(getApplicationContext(), TagFragment.class.getName(), args);
        } else if (KeyStore.TARGET_CATEGORY_TAG_GAME.equals(tag)) {
            result = Fragment.instantiate(getApplicationContext(), TagFragment.class.getName(), args);
        } else if (KeyStore.TARGET_GENIUS.equals(tag)) {
            result = Fragment.instantiate(getApplicationContext(), AppGeniusFragment.class.getName(), args);
        } else if (KeyStore.TARGET_TOPICS.equals(tag)) {
            args.putString(Constants.KEY_TOPIC_ID, getIntent().getStringExtra(Constants.KEY_TOPIC_ID));
            result = Fragment.instantiate(getApplicationContext(), TopicFragment.class.getName(), args);
        }

        return result;
    }
    
    /**
     * 检查是否得到了Tag
     */
    private boolean checkTag(){
        Intent intent = getIntent();
        if(intent != null){
            mCurTag = intent.getStringExtra(KeyStore.KEY_ENTRY);
        }
        return mCurTag != null;
    }

    protected void setActionBarText(String tag) {
        if (KeyStore.TARGET_CATEGORY_APP.equals(tag)) {
            getSupportActionBar().setTitle(R.string.sort_top_title);
        } else if (KeyStore.TARGET_CATEGORY_GAME.equals(tag)) {
            getSupportActionBar().setTitle(R.string.sort_top_title_game);
        } else if (KeyStore.TARGET_CATEGORY_TAG_APP.equals(tag)) {
            getSupportActionBar().setTitle(R.string.category_tag_genius);
        } else if (KeyStore.TARGET_CATEGORY_TAG_GAME.equals(tag)) {
            getSupportActionBar().setTitle(R.string.category_tag_genius);
        } else if (KeyStore.TARGET_GENIUS.equals(tag)) {
            getSupportActionBar().setTitle(R.string.sort_guess_you_want);
        } else if (KeyStore.TARGET_TOPICS.equals(tag)) {
            getSupportActionBar().setTitle(getIntent().getStringExtra(
                    Constants.KEY_TOPIC_NAME));
        }
    }
}
